﻿@{
    Layout = null;
}

<div>
    <div class="col-3 pr-0" style="display: none">
        <label id="AddUsageRecord-EqId">@ViewData["EqId"]</label>
        <label id="AddUsageRecord-Parent-ModalID">@ViewData["ModalID"]</label>
    </div>
    <div id="Import">
        <div class="input-group">
            <div class="col-3 pr-0">
                <span class="input-group-text font-rfs border-radius-noright noRightBorder">设备类型*</span>
            </div>
            <div class="col-9 pl-0 input-group position-relative">
                <select id="AddUsageRecord-SelectType" class="form-control font-rfs border-radius-noleft"></select>
            </div>
        </div>
        <div class="input-group">
            <div class="col-3 pr-0">
                <span class="input-group-text font-rfs border-radius-noright noRightBorder">设备编号*</span>
            </div>
            <div class="col-9 pl-0 input-group position-relative">
                <select id="AddUsageRecord-SelectEqId" class="form-control font-rfs border-radius-noleft"></select>
            </div>
        </div>
        <div class="input-group">
            <div class="col-3 pr-0">
                <span class="input-group-text font-rfs border-radius-noright noRightBorder">数据采集*</span>
            </div>
            <div class="input-group col-9 pl-0">
                <input id="AddUsageRecord-file" type="file" class="form-control font-rfs border-radius-noleft" style="width: 85%; padding-top: 4px;" />
                <button id="AddUsageRecord-UploadFile" class="input-group-text font-rfs border-radius-noleft noLeftBorder" style="width: 15%; padding-top: 0px; padding-bottom: 0px; margin-bottom: 1px;" title="上传数据文件">
                    <i class="bi-cloud-upload font-rfs" style="font-size:18px"></i>
                </button>
            </div>
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">开始时间*</span>
        </div>
        <div class="col-9 pl-0 input-group position-relative">
            <input id="AddUsageRecord-StarTime" type="text" class="dtp form-control font-rfs border-radius-noleft" />
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">结束时间*</span>
        </div>
        <div class="col-9 pl-0 input-group position-relative">
            <input id="AddUsageRecord-EndTime" type="text" class="dtp form-control font-rfs border-radius-noleft" />
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">备注</span>
        </div>
        <div class="col-9 pl-0">
            <input id="AddUsageRecord-Note" type="text" class="form-control font-rfs border-radius-noleft" placeholder="可选"></input>
        </div>
    </div>
    <div class="text-right">
        <button id="AddUsageRecord-confirm" class="btn btn-primary font-rfs">确定</button>
        <button id="AddUsageRecord-cancel" data-dismiss="modal" type="button" class="btn btn-info font-rfs">取消</button>
    </div>
</div>

<script>
    var FileID = null;
    $(document).ready(function () {
        if ($("#AddUsageRecord-EqId").html().length != 0) {
            $("#Import").css("display", 'none');
        } else {
            LoadEquipmentTypeList('AddUsageRecord-SelectType');
            $('#AddUsageRecord-StarTime').attr("disabled", true);
            $('#AddUsageRecord-EndTime').attr("disabled", true);
        }

        $('.dtp').datetimepicker({
            timepicker: true,
            format: "Y/m/d H:i",
            setLocale:'zh'
        });
    });

    $("#AddUsageRecord-SelectType").change(function () {
        LoadEquipmentByType('AddUsageRecord-SelectEqId', $("#AddUsageRecord-SelectType").val());
    });

    $("#AddUsageRecord-confirm").click(function () {
        if ($("#AddUsageRecord-StarTime").val().length == 0) {
            alert("开始时间不能为空！");
            $("#AddUsageRecord-StarTime").addClass('error-style');
            return false;
        }
        if ($("#AddUsageRecord-EndTime").val().length == 0) {
            alert("结束时间不能为空！");
            $("#AddUsageRecord-EndTime").addClass('error-style');
            return false;
        }

        var jsonStr = {};
        if (FileID == null) {
            jsonStr["EqId"] = $("#AddUsageRecord-EqId").html();
        } else {
            jsonStr["EqId"] = $("#AddUsageRecord-SelectEqId").val();
        }
        jsonStr["StartTime"] = $.trim($("#AddUsageRecord-StarTime").val());
        jsonStr["EndTime"] = $.trim($("#AddUsageRecord-EndTime").val());
        jsonStr["Note"] = $.trim($("#AddUsageRecord-Note").val());
        jsonStr["FileID"] = FileID;

        $.post("/Common/CreatUsageRecord", {
            jsonStr: JSON.stringify(jsonStr)
        }, function (data) {
            if (data.indexOf("成功") >= 0) {
                $("#AddUsageRecord-cancel").click();
            } else {
                alert(data);
            }
        });
    });

    $("#AddUsageRecord-cancel").click(function () {
        var modalID = $("#AddUsageRecord-Parent-ModalID").html();
        $("#" + modalID).modal('hide');
    });

    $("#AddUsageRecord-UploadFile").click(function (event) {
        if ($("#AddUsageRecord-SelectEqId").val() == null) {
            alert("请先选择设备！");
            $("#AddUsageRecord-SelectEqId").addClass('error-style');
            return false;
        }

        var formData = new FormData();
        var name = $("#AddUsageRecord-file").val();
        $.each($("#AddUsageRecord-file")[0].files, function (index, value) { // 支持多文件上传
            formData.append('file', $("#AddUsageRecord-file")[0].files[index]);
        });
        formData.append("Domain", 'ImportData'); // 上传文件时附加的参数
        formData.append("DomainId", $('#AddUsageRecord-SelectEqId').val()); 

        $.ajax({
            url: '/Common/UploadFileSave',
            type: 'POST',
            data: formData,
            async: true,
            cache: false,
            processData: false,// 告诉jQuery不要去处理发送的数据
            contentType: false,// 告诉jQuery不要去设置Content-Type请求头
            success: function (data) {
                $("#AddUsageRecord-file").val("");
                $('#AddUsageRecord-StarTime').attr("disabled", false);
                $('#AddUsageRecord-EndTime').attr("disabled", false);
                FileID = data.split(',')[0];
                alert(data.split(',')[1]);
            },
            error: function (returndata) {
                //alert(returndata);
            }
        });
    });

</script>
